{
 "cells": [],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [
     "# 离散时间傅里叶变换的 python 实现\n",
     "import numpy as np\n",
     "import math\n",
     "import pylab as pl\n",
     "import scipy.signal as signal\n",
     "import matplotlib.pyplot as plt\n",
     "\n",
     "sampling_rate=1000\n",
     "t1=np.arange(0, 10.0, 1.0/sampling_rate)\n",
     "x1 =np.sin(15*np.pi*t1)\n",
     "\n",
     "# 傅里叶变换\n",
     "def fft1(xx):\n",
     "#     t=np.arange(0, s)\n",
     "    t=np.linspace(0, 1.0, len(xx))\n",
     "    f = np.arange(len(xx)/2+1, dtype=complex)\n",
     "    for index in range(len(f)):\n",
     "        f[index]=complex(np.sum(np.cos(2*np.pi*index*t)*xx), -np.sum(np.sin(2*np.pi*index*t)*xx))\n",
     "    return f\n",
     "\n",
     "# len(x1)\n",
     "xf=fft1(x1)/len(x1)\n",
     "freqs = np.linspace(0, sampling_rate/2, len(x1)/2+1)\n",
     "plt.figure(figsize=(16,4))\n",
     "plt.plot(freqs,2*np.abs(xf),'r--')\n",
     "\n",
     "plt.xlabel(\"Frequency(Hz)\")\n",
     "plt.ylabel(\"Amplitude($m$)\")\n",
     "plt.title(\"Amplitude-Frequency curve\")\n",
     "\n",
     "plt.show()\n",
     "plt.figure(figsize=(16,4))\n",
     "plt.plot(freqs,2*np.abs(xf),'r--')\n",
     "\n",
     "plt.xlabel(\"Frequency(Hz)\")\n",
     "plt.ylabel(\"Amplitude($m$)\")\n",
     "plt.title(\"Amplitude-Frequency curve\")\n",
     "plt.xlim(0,20)\n",
     "plt.show()\n",
     "\n",
     "\n"
    ],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}